package gov.kqqzyyy.monkey.orderpad.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.mytools.MyStringUtil;
import gov.kqqzyyy.monkey.orderpad.object.SelectedDish;


/**
 * Created by Monkey on 2017/9/11.
 * 已选菜品适配器
 */

public class OrderSelectedAdapter extends RecyclerView.Adapter<OrderSelectedAdapter.MyViewHolder> {

    private final LayoutInflater inflater;
    private List<SelectedDish> list;

    public OrderSelectedAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<SelectedDish> list) {
        this.list = list;
        Collections.sort(list, (arg0, arg1) -> arg0.getID() - arg1.getID());
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_order_selected, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String text = MyStringUtil.doHideCPMC(list.get(position).getCPMC()) + " * " + list.get(position).getCPSL();
        holder.textView.setText(text);
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView textView;

        MyViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.order_dish_selectnum);
        }

    }
}
